<?php

/**
 * Project: CrazyPoems
 * Author: Alexandru
 * Date: 30/10/2008
 * File: contact.php
 */

// Load default settings.
require('inc/config.php');
require('inc/functions.misc.php');
require('inc/functions.index.php');
include_once 'inc/securimage/securimage.php';
$securimage = new Securimage();

$smarty->assign('pageTitle', l(Contacto));
$smarty->assign('breadcrumbs', '<a href="'.BASE_URL.'">'.l('Inicio').'</a> &raquo; '.l('Contacto'));

// Define variables.
$error_msg = "";
$fullname = isset($_POST['fullname']) && strlen($_POST['fullname']) > 0 ? $_POST['fullname'] : '';
$email = isset($_POST['email']) && strlen($_POST['email']) > 0 ? $_POST['email'] : '';
$subject = isset($_REQUEST['subject']) && strlen($_REQUEST['subject']) > 0 ? $_REQUEST['subject'] : '';
$message = isset($_POST['message']) && strlen($_POST['message']) > 0 ? $_POST['message'] : '';

// Validate data.
if (isset($_POST['form_sent']) && $_POST['form_sent'] == "yes") {
  if (strlen($fullname) == 0) {
    $error_msg .= "<p>Introdu numele.</p>";
  }
  if (strlen($email) == 0) {
    $error_msg .= "<p>Introdu e-mail.</p>";
  } else {
    if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\.\\+=_-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email)) {
      $error_msg .= "<p>".l('Dirección de e-mail no válida.')."</p>";
    }
  }
  if (strlen($message) == 0) {
    $error_msg .= "<p>".l('Escriba su mensaje.')."</p>";
  }

  if (strlen($cookie->username) == 0) {
    if ($securimage->check($_POST['captcha_code']) == false) {
     $error_msg .= "<p>".l('El código de verificación introducido es incorrecto.')."</p>";
    }
  }

  // Send contact email.
  if (strlen($error_msg) == 0) {
    $subject = "CrazyPoems Contact: " . $subject;
    if (mail(EMAIL_RECIEVE_CONTACT, $subject, $message, "From: \"".$fullname."\"<".$email.">\nReply-to: \"".$fullname."\"<".$email.">")) {
      // echo 'email sent';
    } else {
      // echo 'email not sent';
    }
  }
} else {
  if (isset($cookie->username)) {
    $fullname = $cookie->fullname;
    $email = $cookie->email;
  }
}

// Smarty.
$smarty->assign('form_sent', $_POST['form_sent']);
$smarty->assign('error_msg', $error_msg);
$smarty->assign('fullname', $fullname);
$smarty->assign('email', $email);
$smarty->assign('subject', $subject);
$smarty->assign('message', $message);
$smarty->display('contact.tpl');

// End page.
require('inc/end.php');
?>
